package jwxx.server;
import java.sql.ResultSet;
import java.sql.SQLException;
import jwxx.server.DbOperator;
import jwxx.server.SItemEffect;
public class SItemEffectSql {
	private int n;
	private ResultSet rs;
	
	public int insert(SItemEffect cb)throws SQLException{
		String sql = "insert into item_effect_table (item_id,metal,wood,water,fire,earth,dam_min,dam_max,item_type,gem_1,gem_2,gem_3,direct_hit)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object [] obj = new Object []{cb.getItemID(),cb.getMetal(),cb.getWood(),cb.getWater(),cb.getFire(),cb.getEarth(),cb.getDamMin(),cb.getDamMax(),cb.getItemType(),cb.getGem1(),cb.getGem2(),cb.getGem3(),cb.getDirectHit()};
				
		DbOperator db = new DbOperator();
		try	{
			n=db.executeUpdate(sql,obj);
			}catch(SQLException e){
				e.printStackTrace();
			}
		db.close();	
		return n;
		}

	public int delete(int item_id)throws SQLException{
		String sql = "delete from item_effect_table where item_id=?";
		Object [] obj = new Object []{new Integer(item_id)};
		DbOperator db = new DbOperator();		
		try	{
			n=db.executeUpdate(sql,obj);
			}catch(SQLException e){
				e.printStackTrace();
			}	
		db.close();	
		return n;
	}	

	public int update(SItemEffect cb)throws SQLException{
		String sql = "update item_effect_table set metal=?,wood=?,water=?,fire=?,earth=?,dam_min=?,dam_max=?,item_type=?,gem_1=?,gem_2=?,gem_3=?,direct_hit=? where item_id=?";
		Object [] obj = new Object []{cb.getMetal(),cb.getWood(),cb.getWater(),cb.getFire(),cb.getEarth(),cb.getDamMin(),cb.getDamMax(),cb.getItemType(),cb.getGem1(),cb.getGem2(),cb.getGem3(),cb.getDirectHit(),new Integer(cb.getItemID())};
		DbOperator db = new DbOperator();	
		try	{
			n=db.executeUpdate(sql,obj);
			}catch(SQLException e){
				e.printStackTrace();
			}	
		db.close();	
		return n;		
	}	
	
	public ResultSet selectAllClass()throws SQLException{
		String sql="select * from item_effect_table";
		DbOperator db = new DbOperator();
		try{
			rs=db.executeQuery(sql, new Object[]{});
		}catch(SQLException e){
			e.printStackTrace();
		}	
		db.close();
		return rs;
	}
	
	public SItemEffect selectById(int item_id)throws SQLException{		
		String sql2 = "select * from item_effect_table where item_id=?";
		DbOperator db = new DbOperator();
		SItemEffect cb = new SItemEffect();
		try{
			rs = db.executeQuery(sql2, new Object[]{new Integer(item_id)});
			while(rs.next()){
				cb.setItemID(rs.getInt("item_id"));
				cb.setMetal(rs.getInt("metal"));
				cb.setWood(rs.getInt("wood"));
				cb.setWater(rs.getInt("water"));		
				cb.setFire(rs.getInt("fire"));
				cb.setEarth(rs.getInt("earth"));				
				cb.setDamMin(rs.getInt("dam_min"));
				cb.setDamMax(rs.getInt("dam_max"));		
				cb.setItemName(rs.getString("item_name"));
				cb.setItemType(rs.getInt("item_type"));	
				cb.setGem1(rs.getInt("gem_1"));
				cb.setGem2(rs.getInt("gem_2"));		
				cb.setGem3(rs.getInt("gem_3"));		
				cb.setDirectHit(rs.getInt("direct_hit"));
			
			}
		}catch(SQLException e){
			e.printStackTrace();
		}	
		db.close();
		return cb;
	
		}

}
